package com.chaosj.skill.guava

import com.google.common.collect.Lists
import com.google.common.util.concurrent.RateLimiter

/**
 * @Description:
 * @author: caoyangjie
 * @date: 2019年11月2019/11/18日 14:09
 * @Copyright: © 赢时胜
 */
class GuavaRateLimit {
    static void main(String[] args){
        def limiter = RateLimiter.create(5)
        def tasks = Lists.newArrayListWithCapacity(20)
        for (int i = 0; i < 20; i++) {
            tasks.add(new Request(i+""))
        }
        tasks.stream().forEach({task->
            System.out.println("等待时间："+limiter.acquire())
            task.run()
        })
    }

    static class Request implements Runnable{
        String parameter

        Request(String parameter) {
            this.parameter = parameter
        }

        @Override
        void run() {
            System.err.println(parameter)
        }
    }
}
